package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etoak.config.EmailQueueConfig;
import com.etoak.entity.User;
import com.etoak.mapper.UserMapper;
import com.etoak.service.USerService;
import com.etoakk.common.bean.Email;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements USerService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void registry(User user) {
        user.setPassword(MD5.create().digestHex("123456"));
        user.setCreateTime(DateUtil.now());
        this.save(user);
        //fasongxiaoxi
        Email email=new Email(user.getEmail(),
                "小b克",
                "别吃肉夹馍了");
        String msg = JSONUtil.toJsonStr(email);
        rabbitTemplate.convertAndSend(EmailQueueConfig.EXCHANGE,EmailQueueConfig.KEY,msg);
    }
}
